package com.zhou.demo.interfaceclass;

import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.zhou.demo.callback.ThemesCallback;
import com.zhou.demo.url.ZhihuUrl;

import java.io.IOException;

/**
 * Created by 11065 on 2016/8/11.
 */
public class ThemeConnect implements ThemeInterface{
    @Override
    public void getThemes(final ThemesCallback callback) {
        Request request = new Request.Builder()
                .url(ZhihuUrl.THEMEFORZHIHU)
                .build();
        OkHttpClient client = new OkHttpClient();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                callback.onThemesFail(e.getMessage());
            }

            @Override
            public void onResponse(Response response) throws IOException {
                callback.onThemesSuccess(response.body().string());
            }
        });
    }

    @Override
    public void getThemesInfo(String id, final ThemesCallback callback) {
        Request request = new Request.Builder()
                .url(ZhihuUrl.THEMEFORZHIHU+"/"+id)
                .build();
        OkHttpClient client = new OkHttpClient();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                callback.onThemesFail(e.getMessage());
            }

            @Override
            public void onResponse(Response response) throws IOException {
                callback.onThemesSuccess(response.body().string());
            }
        });
    }
    
}

interface ThemeInterface{
    void getThemes(ThemesCallback callback);
    void getThemesInfo(String id,ThemesCallback callback);
}
